package com.four.commodity.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.four.commodity.pojo.Commodity;
import com.four.commodity.service.CommodityService;
import com.four.commodity.service.impl.CommodityServiceImpl;
import com.google.gson.Gson;

@WebServlet("/selectCommodityById.do")
public class SelectCommodityByidServiet extends HttpServlet{
//通过id查询
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			req.setCharacterEncoding("utf-8");
			resp.setCharacterEncoding("utf-8");
			String  id = req.getParameter("id");
			CommodityService service = new CommodityServiceImpl();
			Commodity c = service.selectById(id);
			resp.setContentType("application/json");
			PrintWriter pw = resp.getWriter();
			Gson gson = new Gson();
			String result = gson.toJson(c);
			pw.append(result);
			pw.flush();
			pw.close();
			
		}
}
